<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head>
	<th:block th:include="include :: bootstrap-editable-css" />
	<th:block th:include="include :: header('新建入库单')"/>
	<th:block th:include="include :: layout-latest-css"/>
	<th:block th:include="include :: ztree-css"/>
</head>
<body class="gray-bg">
    <div class="container-div">
		<div class="col-sm-12 search-collapse">
			<form id="in-form">
				<div class="select-list">
					<ul>

<!--						<li>-->
<!--							<label>仓库：</label>-->
<!--							<select name="warehouseId" th:with="type=${@dict.getType('is_house')}" required>-->
<!--								&lt;!&ndash;<option value="">请选择</option>&ndash;&gt;-->
<!--								<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>-->
<!--							</select>-->
<!--						</li>-->

<!--						<li>-->
<!--							<label class="is-required">仓库：</label>-->
<!--							<select id="warehouseId" name="warehouseId" th:with="type=${session.warehouse}">-->
<!--&lt;!&ndash;								<option   th:value="${session.warehouseId}" th:text="${@dict.getDictLabel('${session.warehouseId}')}"></option>&ndash;&gt;-->
<!--								<option th:each="dict : ${type}" th:text="${dict.warehouseName}" th:value="${dict.warehouseId}"></option>-->
<!--							</select>-->
<!--						</li>-->
						<li>
							<label class="is-required">入库类型：</label>
							<select id="inType" name="inType" th:with="type=${@dict.getType('dj_type')}">
								<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
							</select>
						</li>
						<!--<li>-->
							<!--<label class="is-required">物料来源：</label>-->
							<!--<select name="customerId">-->
								<!--<option value="0">部门</option>-->
								<!--<option value="1">车间</option>-->
								<!--<option value="2">供应商</option>-->
							<!--</select>-->
						<!--</li>-->

						<li>
							<label>建单时间：</label><input type="text" id="createDate" name="createDate" readonly/>
						</li>
						<li>
							<label>单据号：</label><input type="text" placeholder="系统自动生成单据编号" id="stockinId" readonly>
						</li>
<!--						<li>-->
<!--							<lable class="is-required">供应商：</lable>-->
<!--							<input id="supplierId" name="supplierId" hidden>-->
<!--							<input id="supplierName" name="supplierName" >-->
<!--							<a id="selectSupplier" class="btn btn-success" onclick="selectSupplier()" >-->
<!--								...-->
<!--							</a>-->
<!--						</li>-->
						<li>
							<a id="save" class="btn btn-primary btn-rounded btn-sm" onclick="saveOutStock()"><i
									class="fa fa-search"></i>&nbsp;保存单据</a>
							<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
									class="fa fa-refresh"></i>&nbsp;重置</a>
						</li>
					</ul>
				</div>
			</form>
		</div>
		<div class="btn-group-sm" id="toolbar" role="group">
			<a id="selectProduct" class="btn btn-success" onclick="selectProduct()" >
				<i class="fa fa-plus"></i> 选择物资
			</a>
			<a id="continue" class="btn btn-success" onclick="newReceiptAgain()" >
				<i class="fa fa-plus"></i> 继续建单
			</a>
			<a id="delete" class="btn btn-danger multiple disabled" onclick="deleteProduct()" >
				<i class="fa fa-remove"></i> 删除物资
			</a>

		</div>
			
		<div id="div" class="col-sm-12 select-table table-striped">
			<table id="bootstrap-table" data-mobile-responsive="true"></table>
		</div>
    </div>
	<th:block th:include="include :: footer"/>
	<th:block th:include="include :: bootstrap-table-editable-js" />
	<th:block th:include="include :: layout-latest-js"/>
	<th:block th:include="include :: ztree-js"/>
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('mydo:stockin:edit')}]];
        var removeFlag = [[${@permission.hasPermi('mydo:stockin:remove')}]];
		var productStatus = [[${@dict.getType('product_status')}]];
        var prefix = ctx + "in/stockin"

		// var sessionParam = '[[${session.sessionParam}]]';

		$(function () {
			var options = {
				// url: "/out/stockoutview/list",
				createUrl: prefix + "/addSave",
				search:false,
				clickToSelect: true,
				contenteditable:true,
				height: $("#div").height()/1,
				modalName: "入库子单",
				editable:true,
				columns: [{
					checkbox: true,

				},
					{
						field: 'productGuid',
						title: 'ID',
						visible: false,
					},
					{
						field: 'productId',
						title: '物资编码'
					},{
						field: 'productName',
						title: '物资名称'
					},
					{
						field: 'unit',
						title: '单位',
						visible: true
					},

					{
						field: 'quantity',
						title: '数量',
						visible: true,
						editable: {
							type: 'text',
							title: '数量',
							disabled : false, // 是否禁用编辑
							validate: function (value) {
								if (isNaN(value)) {
									return '请填写数字';
								}
								if (value.length == 0) {
									return '数量不能为空';
								}
							}

						},
						formatter: function (value, row, index) {
							if (value) {
								return value;
							} else {
								row.quantity = '';
								return '请填写数量';
						}
						}
					},
					// {
					// 	field: 'assignNum',
					// 	title: '重量',
					// 	visible: true,
					// 	editable: {
					// 		type: 'text',
					// 		title: '重量',
					// 		disabled : false, // 是否禁用编辑
					// 		validate: function (value) {
					// 			if (isNaN(value)) {
					// 				return '请填写数字';
					// 			}
					// 			if (value.length == 0) {
					// 				return '重量不能为空';
					// 			}
					// 		}
					//
					// 	},
					// 	formatter: function (value, row, index) {
					// 		if (value) {
					// 			return value;
					// 		} else {
					// 			row.assignNum = '';
					// 			return '请填写重量';
					// 	}
					// 	}
					// },
					{
						field: 'lotNo',
						title: '批号',
						visible: true,
						editable: {
							type: 'text',
							title: '批号',
							disabled: false,
							// validate: function (value) {
							// 	// if (value.length == 0) {
							// 	// 	return '批号不能为空';
							// 	// }
							// }
						},
						// formatter: function (value, row, index) {
						// 	if (value) {
						// 		return value;
						// 	} else {
						// 		row.lotNo = '';
						// 		return '请填写批号';
						// 	}
						// }
					},
					// {
					// 	field: 'stockStatus',
					// 	title: '物料状态',
					// 	visible: true,
					// 	editable: {
					// 		type: 'select',
					// 		title: '物料状态',
					// 		source: function () {
					// 			var result = [];
					// 			$.each(productStatus, function (key, value) {
					// 				result.push({ value: value.dictValue, text: value.dictLabel });
					// 			});
					// 			return result;
					// 		}
					// 	},
					// 	formatter: function (value, row, index) {
					// 		if (value) {
					// 			return value;
					// 		} else {
					// 			row.stockStatus = '';
					// 			return '请填写物料状态';
					// 		}
					// 	}
					// },
					// {
					// 	field: 'printDate',
					// 	title: '生产时间',
					// 	visible: true,
					// 	editable: {
					// 		type: 'text',
					// 		title: '生产时间',
					// 		disabled: false,
					// 		validate: function (value) {
					// 			if (value.length == 0) {
					// 				return '生产时间不能为空';
					// 			}
					// 		}
					// 	},
					// 	formatter: function (value, row, index) {
					// 		if (value) {
					// 			return value;
					// 		} else {
					// 			row.printDate ='';
					// 			return '请填写生产时间';
					// 		}
					// 	}
					// },
					// {
					// 	field: 'supplierId',
					// 	title: '供应商',
					// 	visible: true,
					// 	editable: {
					// 		type: 'text',
					// 		title: '供应商',
					// 		disabled: false,
					// 		validate: function (value) {
					// 			if (value.length == 0) {
					// 				return '供应商不能为空';
					// 			}
					// 		}
					// 	},
					// 	formatter: function (value, row, index) {
					// 		if (value) {
					// 			return value;
					// 		} else {
					// 			row.supplierId = '';
					// 			return '请填写供应商';
					// 		}
					// 	}
					// },{
					// 	field: 'cargoNo',
					// 	title: '托盘条码',
					// 	visible: true,
					// 	editable: {
					// 		type: 'text',
					// 		title: '托盘条码',
					// 		disabled: false,
					// 		validate: function (value) {
					// 			if (value.length == 0) {
					// 				return '托盘条码不能为空';
					// 			}
					// 		}
					// 	},
					// 	formatter: function (value, row, index) {
					// 		if (value) {
					// 			return value;
					// 		} else {
					// 			row.cargoNo = '';
					// 			return '请填写托盘条码';
					// 		}
					// 	}
					// },
					{
						field: 'remark',
						title: '备注',
						visible: true,
						editable: {
							type: 'text',
							title: '备注',
							disabled : false, // 是否禁用编辑
							validate: function (value) {
							}
						},
						// formatter: function (value, row, index) {
						// 	if (value) {
						// 		return value;
						// 	} else {
						// 		row.remark = '';
						// 		return '请填写备注';
						// 	}
						// }
					},
				]
			};
			$.table.init(options);
			createTime();
		});
		//日期框
		function createTime(){
			var _input = document.getElementById('createDate');

			var date = new Date();
			var seperator = "";
			var year = date.getFullYear();
			var month = date.getMonth() + 1;
			var strDate = date.getDate();
			if (month >= 1 && month <= 9) {
				month = "0" + month;
			}
			if (strDate >= 0 && strDate <= 9) {
				strDate = "0" + strDate;
			}
			var currentDate = year + seperator + month + seperator + strDate; //当前日期

			_input.value = currentDate; //赋值给input.value
		}
		//选择物资
		function selectProduct() {
			var options = {
				title: '选择物品信息',
				url: "/in/stockin/selectPro",
				callBack: getMembers
			};
			$.modal.openOptions(options);

		}

		function getMembers(index, layero) {

			var row = layero.find("iframe")[0].contentWindow.submitHandler();
			if (row.length == 0) {
				$.modal.alertWarning("请至少选择一条记录");
				return;
			}
			var zTreeDoorData = [];
			zTreeDoorData.splice(0, zTreeDoorData.length);
			for (var i = 0; i < row.length; i++) {
				treedoor = {
					productGuid: row[i].productGuid,
					productId: row[i].productId,
					// stocStatus: row[i].productClassification,
					productName: row[i].productName,
					unit: row[i].productUnit,
					spec: row[i].spec,
					model: row[i].model,
					productFlag: row[i].productFlag,
					specialFlag: row[i].specialFlag,
					proportionNum: row[i].proportionNum,
				}
				zTreeDoorData.push(treedoor);
			} //forEnd

			for (var j = 0; j < zTreeDoorData.length; j++) {
				var dataTree2 = zTreeDoorData[j];
				$('#bootstrap-table').bootstrapTable('append', dataTree2);
			}
			$.modal.close(index);
			$.modal.closeLoading();
		}

		//保存单据
		function saveOutStock() {
			var rows= $('#bootstrap-table').bootstrapTable('getData');
			if(rows.length==0){
				$.modal.alertWarning("请选择物资");
				return;
			}
			console.log(rows)
			for(var i=0;i<rows.length;i++) {
				if(rows[i].quantity==""){
					$.modal.alertWarning("请填写入库数量")
					return;
				}if(rows[i].lotNo==""){
					$.modal.alertWarning("请填写批号")
					return;
				}if(rows[i].stockStatus==""){
					$.modal.alertWarning("请填写物品状态")
					return;
				}if(rows[i].printDate==""){
					$.modal.alertWarning("请填写生产时间")
					return;
				}
				var inType = $("#inType").val();
				if(inType=="9" && rows[i].cargoNo == ""){
					console.log(inType)
					$.modal.alertWarning("暂存单请填写托盘号")
					return;
				}
				var warehouseId = $("#warehouseId").val();
				if(warehouseId == "" ){
					$.modal.alertWarning("请填写仓库信息")
					return;
				}
			}

			if($.validate.form()){
				var params=new Object();
				let obj = {'inType': $("#inType").val(),
					       'createDate': $("#createDate").val(),
					       'supplierId':$("#supplierId").val(),
				            'warehouseId':$("#warehouseId").val()};
				params.stockin=obj;
				params.stockinDetailList=$("#bootstrap-table").bootstrapTable("getData",false);
				var dada=JSON.stringify(params);

				var opt=new Object()

				opt.url = '/in/stockin/addSave';
				opt.data=JSON.stringify(params);

				ajaxRequestJsonBody(opt,onSaveSuccess);
				function ajaxRequestJsonBody(opt,callback){

					$.ajax({
						url			: opt.url,
						/**必须是POST方法**/
						type		: 'post',
						/**必须制定请求的类型**/
						contentType :'application/json',
						data		: opt.data,
						dataType	: 'json',
						success : function(result) {
							console.log(result)
							if (result.code == "0") {
								$.modal.alertSuccess(result.msg);
							} else if (result.code == "301") {
								$.modal.alertWarning(result.msg);
							}  else {
								$.modal.alertError(result.msg);
							}
							if(callback)
								callback(result);
						},
						error:function(XMLHttpRequest,textStatus,error){
							DataTable.config.ajaxError(XMLHttpRequest,textStatus,error,"设置失败");
						}
					});
				}
			}
		}
		//保存单据后的回调函数
		function onSaveSuccess(data) {
			if(data.code == web_status.SUCCESS){
				$('#stockinId').attr("value",data.data);
				$('#save').addClass('disabled'); // Disables visually
				$('#selectProduct').addClass('disabled'); // Disables visually
				$('#delete').addClass('disabled'); // Disables visually
				$('#reset').addClass('disabled');

			}
		}
		//继续建单
		function newReceiptAgain(){
			window.location.reload();
			$.form.reset()
		}

		//删除物资
		function deleteProduct() {
			//获取选中行的内容
			var contents = $.map($("#bootstrap-table").bootstrapTable('getSelections'), function (row) {
				return row.productGuid;
			});
			//前端删除数据
			$("#bootstrap-table").bootstrapTable('remove', {
				field: 'productGuid',
				values: contents
			});
		}

		// 选择供应商
		function selectSupplier(){

			var options = {
				title: '选择物品信息',
				url: "/in/stockin/selectSupplier",
				callBack: getSupplier
			};
			$.modal.openOptions(options);

		}

		// 关闭供应商框
		function getSupplier(index, layero) {

			var row = layero.find("iframe")[0].contentWindow.submitHandler();
			if (row.length == 0) {
				$.modal.alertWarning("请至少选择一条记录");
				return;
			}

			var zTreeDoorData1 = [];
			zTreeDoorData1.splice(0, zTreeDoorData1.length);
			for (var i = 0; i < row.length; i++) {
				$('#supplierId').val(row[i].supplierNo);
				$('#supplierName').val(row[i].supplierName);

			}
			$.modal.close(index);
			$.modal.closeLoading();

		}
    </script>
</body>
</html>